package cn.yx.framework.security;

import cn.yx.framework.util.HexUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

public class Des3Kit {
    private static final String TAG = "Des3Kit";
    private static final Logger logger = LoggerFactory.getLogger(Des3Kit.class);
    private static byte[] SECRET_KEY;
    private static final String IV = "^B_0f`:i";
    private static final String CHARSET_NAME = "UTF-8";
    public static final String DESEDE_CBC_PKCS5_PADDING = "desede/CBC/PKCS5Padding";
    private static SecretKeyFactory KEY_FACTORY;

    public Des3Kit() {
    }

    public static String encrypt(String plaintext) {
        try {
            DESedeKeySpec e = new DESedeKeySpec(SECRET_KEY);
            Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding");
            IvParameterSpec ips = new IvParameterSpec("^B_0f`:i".getBytes());
            cipher.init(1, KEY_FACTORY.generateSecret(e), ips);
            byte[] encryptData = cipher.doFinal(plaintext.getBytes("UTF-8"));
            return HexUtil.bytes2HexStr(encryptData);
        } catch (Exception var5) {
            throw new RuntimeException(var5);
        }
    }

    public static String decrypt(String ciphertext) {
        try {
            DESedeKeySpec ex = new DESedeKeySpec(SECRET_KEY);
            Cipher cipher = Cipher.getInstance("desede/CBC/PKCS5Padding");
            IvParameterSpec ips = new IvParameterSpec("^B_0f`:i".getBytes());
            cipher.init(2, KEY_FACTORY.generateSecret(ex), ips);
            byte[] decryptData = cipher.doFinal(HexUtil.hexChar2Byte(ciphertext.toCharArray()));
            return new String(decryptData, "UTF-8");
        } catch (Exception var5) {
            logger.error("Des3Kit", "数据DES3解密错误", var5);
            throw new RuntimeException(var5);
        }
    }

    public static String compress(String source) {
        if(source != null && source.length() != 0) {
            try {
                ByteArrayOutputStream ex = new ByteArrayOutputStream();
                GZIPOutputStream gzip = new GZIPOutputStream(ex);
                gzip.write(source.getBytes());
                gzip.close();
                return ex.toString("ISO-8859-1");
            } catch (IOException var3) {
                logger.error("Des3Kit", "数据压缩错误", var3);
                throw new RuntimeException(var3);
            }
        } else {
            return source;
        }
    }

    public static String decompress(String source) {
        if(source != null && source.length() != 0) {
            try {
                ByteArrayOutputStream ex = new ByteArrayOutputStream();
                ByteArrayInputStream in = new ByteArrayInputStream(source.getBytes("ISO-8859-1"));
                GZIPInputStream gunzip = new GZIPInputStream(in);
                byte[] buffer = new byte[256];

                int n;
                while((n = gunzip.read(buffer)) >= 0) {
                    ex.write(buffer, 0, n);
                }

                return ex.toString();
            } catch (IOException var6) {
                logger.error("Des3Kit", "数据解压错误", var6);
                throw new RuntimeException(var6);
            }
        } else {
            return source;
        }
    }

    static {
        try {
            SECRET_KEY = "1234567890123456789012344".getBytes();
            KEY_FACTORY = SecretKeyFactory.getInstance("desede");
        } catch (NoSuchAlgorithmException var1) {
            logger.error("初始化DES3算法错误", var1);
        }

    }
}
